﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.CC.Utility;
using Garlic.BLL.Common;
using Garlic.DM.DataModel;
using Garlic.CC.WebUtility;
using Garlic.BLL;
using System.Collections.Generic;

public partial class SystemManagement_MenuEdit : BasePage
{
    

    #region Page load
    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {

                if (string.IsNullOrEmpty(Request["RecordID"]))
                {
                    
                    this.MainFormView.DefaultMode = FormViewMode.Insert;
                    DropDownList ddlParentMenu = (DropDownList)(this.MainFormView.FindControl("ddlParentMenuID"));
                    MenuManager.BindMenuList(ddlParentMenu);
                    //新建菜单时，不允许不选择父级菜单 2007-10-18
                    ddlParentMenu.Items.Remove(ddlParentMenu.Items.FindByText("--"));
                }
                else if (Request["OperateType"] == WebUtil.OperateType_Update)
                {
                    
                    this.MainFormView.DefaultMode = FormViewMode.Edit;
                    
                }

                else
                {
                    this.MainFormView.DefaultMode = FormViewMode.ReadOnly;
                    this.TopToolBar.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    #region toolbar event handle
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
            switch (args.Command)
            {
                case ToolbarCommand.Save:
                    SaveInfo();
                    break;
            }
        
    }

    protected void Button1_OnClick(object sender, EventArgs e)
    {
        string aa = "";
    }

    /// <summary>
    /// 保存页面信息
    /// </summary>
    private void SaveInfo()
    {
        try
        {
            if (this.MainFormView.CurrentMode == FormViewMode.Insert)
            {
                this.MainFormView.InsertItem(true);
            }
            else if (this.MainFormView.CurrentMode == FormViewMode.Edit)
            {
                this.MainFormView.UpdateItem(true);
            }

            this.CloseVirtualWindow(true);
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    #endregion

    #region form view control event handle
    /// <summary>
    /// before inserting data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MainFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        try
        {
            this.Validate();

            //请修改下面的初始值:
            e.Values["Menu_name"] = (e.Values["Menu_name"].ToString() == "") ? "" : e.Values["Menu_name"];
            if (!e.Values.Contains("Parent_menu_id"))
            {
                e.Values.Add("Parent_menu_id", ((DropDownList)(MainFormView.FindControl("ddlParentMenuID"))).SelectedValue);
            }
            e.Values["Parent_menu_id"] = (e.Values["Parent_menu_id"].ToString() == "") ? "" : e.Values["Parent_menu_id"];
            e.Values["Order_id"] = (e.Values["Order_id"].ToString() == "") ? "0" : e.Values["Order_id"];
            e.Values["Image_url"] = (e.Values["Image_url"].ToString() == "") ? "" : e.Values["Image_url"];
            e.Values["Link_url"] = (e.Values["Link_url"].ToString() == "") ? "" : e.Values["Link_url"];
            e.Values["Comments"] = (e.Values["Comments"].ToString() == "") ? "" : e.Values["Comments"];
            e.Values["Create_user"] = (e.Values["Create_user"].ToString() == "") ? CurrentUser.UserID : e.Values["Create_user"];
            e.Values["Create_time"] = (e.Values["Create_time"].ToString() == "") ? DateTime.Now : e.Values["Create_time"];
            e.Values["Modify_user"] = (e.Values["Modify_user"].ToString() == "") ? null : e.Values["Modify_user"];
            e.Values["Modify_time"] = (e.Values["Modify_time"].ToString() == "") ? null : e.Values["Modify_time"];
            e.Values["Menu_id"] = (e.Values["Menu_id"].ToString() == "") ? SequenceManager.GetNextVal("MenuID") : e.Values["Menu_id"];

        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    /// <summary>
    /// before updating data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MainFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        try
        {
            if (!e.NewValues.Contains("Parent_menu_id"))
            {
                e.NewValues.Add("Parent_menu_id", ((DropDownList)(MainFormView.FindControl("ddlParentMenuID"))).SelectedValue);
            }
            e.NewValues["Modify_user"] = CurrentUser.UserID;
            e.NewValues["Modify_time"] = DateTime.Now;
            this.Validate();
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    #region tool method
    /// <summary>
    /// show message 
    /// </summary>
    /// <param name="msg"></param>
    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }
    #endregion

    protected void MainFormView_DataBound(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(Request["RecordID"]) && Request["OperateType"] == WebUtil.OperateType_Update)
            {
                DropDownList ddlParentMenuID = this.MainFormView.FindControl("ddlParentMenuID") as DropDownList;
                MenuManager.BindMenuList((DropDownList)(this.MainFormView.FindControl("ddlParentMenuID")));
                MenuInfo menuInfo = this.MainFormView.DataItem as MenuInfo;
                if (ddlParentMenuID.Items.Count > 0)
                {
                    for (int i = 0; i < ddlParentMenuID.Items.Count; i++)
                    {
                        if (ddlParentMenuID.Items[i].Value == menuInfo.Parent_menu_id.ToString())
                        {
                            ddlParentMenuID.Items[i].Selected = true;
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    protected void formViewObjectDataSource_Updated(object sender, ObjectDataSourceStatusEventArgs e)
    {
        this.ShowMessage("成功。");
    }
}


